package cn.vcjf.weapp.api.goods.controller;


import cn.vcjf.weapp.api.goods.entity.GoodsRefundOrder;
import cn.vcjf.weapp.api.goods.service.GoodsRefundOrderService;
import cn.vcjf.weapp.api.payment.qfwxpay.model.VcQfPayRes;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/goods/goodsRefundOrder")
public class GoodsRefundOrderController {
	@Resource
	private GoodsRefundOrderService goodsRefundOrderService;

	/**
	 * 订单退款
	 *
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/refundGoodsOrder")
	@ResponseBody
	public TableResult refundGoodsOrder(GoodsRefundOrder goodsRefundOrder) {

		return goodsRefundOrderService.refundGoodsOrder(goodsRefundOrder);
	}

	/**
	 * 商家同意退款
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/agreeGoodsRefundOrder")
	@ResponseBody
	public TableResult agreeGoodsRefundOrder(GoodsRefundOrder goodsRefundOrder) {
		if(goodsRefundOrder.getId() == null){
			return TableResult.ErrorResult("操作失败:退款订单id不存在");
		}
		VcQfPayRes vcQfPayRes = goodsRefundOrderService.agreeGoodsRefundOrder(goodsRefundOrder);
		if(vcQfPayRes.isOK()){
			return TableResult.OKResult("操作成功");
		}
		return TableResult.ErrorResult("操作失败:"+vcQfPayRes.getErrMsg());
	}
	/**
	 * 商家拒绝退款
	 * @param id
	 * @return
	 */
	@RequestMapping("/sendRefundOrderMessage")
	@ResponseBody
	public TableResult sendRefundOrderMessage(Integer id) {
		if(id == null){
			return TableResult.ErrorResult("操作失败:退款订单id不存在");
		}
		return goodsRefundOrderService.sendRefundOrderMessage(id);
	}

	/**
	 * 商家拒绝退款,用户重新发起退款
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/updateGoodsRefundOrder")
	@ResponseBody
	public TableResult updateGoodsRefundOrder(GoodsRefundOrder goodsRefundOrder) {
		GoodsRefundOrder order = goodsRefundOrderService.selectById(goodsRefundOrder.getId());
		//退款状态(1,待审核,2,已审核,3,拒绝,4申请成功,5已完成,6失败)
		if (order.getStatus() != 3 && order.getStatus() != 6) {
			return TableResult.ErrorResult("操作非法");
		}
		goodsRefundOrder.setStatus(1);
		goodsRefundOrder.preUpdate();
		boolean b = goodsRefundOrderService.updateById(goodsRefundOrder);
		return TableResult.Result(b);
	}

	/**
	 * 获取用户的退款订单详情
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/findGoodsRefundOrderOne")
	@ResponseBody
	public TableResult findGoodsRefundOrderOne(GoodsRefundOrder goodsRefundOrder) {

		EntityWrapper<GoodsRefundOrder> wrapper = goodsRefundOrder.getQueryWrapper();
		//Todo

		GoodsRefundOrder result = goodsRefundOrderService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	/**
	 * 获取用户的退款订单列表
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/findGoodsRefundOrderList")
	@ResponseBody
	public TableResult findGoodsRefundOrderList(GoodsRefundOrder goodsRefundOrder) {

		Page<GoodsRefundOrder> page = goodsRefundOrder.getPageInfo();

		page = goodsRefundOrderService.selectRefundOrderList(page, goodsRefundOrder);

		return TableResult.OKResult(page);

	}

	/**
	 * 用户取消退款
	 *
	 * @param goodsRefundOrder
	 * @return
	 */
	@RequestMapping("/deleteGoodsRefundOrder")
	@ResponseBody
	public TableResult deleteGoodsRefundOrder(GoodsRefundOrder goodsRefundOrder) {
		goodsRefundOrder.preUpdate();
		boolean b = goodsRefundOrderService.deleteById(goodsRefundOrder.getId());
		return TableResult.Result(b);
	}

}
